﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MCB.Net.Protocol
{
    public class HttpProtocolFactory : IProtocolFactory
    {
        ASCIIEncoding encoding = new ASCIIEncoding();
        
        public IProtocol GetProtocol(byte[] buffer)
        {
            string output = encoding.GetString(buffer);

            string[] protocolLines = output.Split(Convert.ToChar("\n"));
            string[] protocolConstituents = protocolLines[0].Split(' ');

            if (protocolConstituents.Length == 3)
            {
                if (protocolConstituents[2].StartsWith("HTTP"))
                {
                    return new HttpProtocol(new HttpRequest()
                    {
                        Command = protocolConstituents[0],
                        Url = protocolConstituents[1],
                        Version = protocolConstituents[2]
                    }, buffer);
                }
            }   
            return null;
        }
    }
}
